package com.digital.welcome.controller;


import com.alibaba.fastjson.JSON;
import com.digital.welcome.annotation.OperationLog;
import com.digital.welcome.entity.PageRequest;
import com.digital.welcome.entity.Permission;
import com.digital.welcome.entity.User;
import com.digital.welcome.response.Result;
import com.digital.welcome.service.UserService;
import com.digital.welcome.token.JwtToken;
import com.digital.welcome.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 韩
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @OperationLog()
    @ApiOperation("登录")//接口信息
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@RequestBody User user, HttpServletRequest request) {
        return userService.login(user, request);

    }

    @OperationLog()
    @ApiOperation("登出")
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout() {

        SecurityUtils.getSubject().logout();
        return JSON.toJSONString(new Result<>().setCode(1).setMessage("成功退出"));

    }

    @ApiOperation("学生信息查询")
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public String selectUser(@RequestBody User user) throws InterruptedException {
        return userService.selectUser(user.getUserName());
    }

    @ApiOperation("个人信息查询")
    @RequiresPermissions("personal")
    @RequestMapping(value = "/selectSelfInformation", method = RequestMethod.GET)
    public String selectSelfInformation(@RequestHeader JwtToken jwtToken) {
        String token = (String) jwtToken.getPrincipal();
        Claims claims = JwtUtil.parseJWT(token);
        assert claims != null;
        String username = claims.getId();
        User user = userService.getUserByUsername(username);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("获取成功").setData(user));

    }

    @ApiOperation("通过id查询用户")
    @RequestMapping(value = "/select-by-id", method = RequestMethod.POST)
    public String selectUserById(@RequestBody User user) {
        return userService.getUserById(user);
    }

    @OperationLog()
    @ApiOperation("单个插入用户")
    @RequestMapping(value = "/user-insert", method = RequestMethod.POST)
    public String userInsert(@RequestBody User user) {
        return userService.insertUser(user);
    }

    @OperationLog()
    @ApiOperation("批量插入用户")
    @RequestMapping(value = "/users-insert", method = RequestMethod.POST)
    public String usersInsert(HttpServletRequest request) throws Exception {
        return userService.insertUsers(request);
    }

    @OperationLog()
    @ApiOperation("获取菜单列表")
    @RequestMapping(value = "/getMenuList", method = RequestMethod.GET)
    public String getMenuList(@RequestHeader JwtToken jwtToken) {
        String token = (String) jwtToken.getPrincipal();
        Claims claims = JwtUtil.parseJWT(token);
        assert claims != null;
        String userName = claims.getId();
        if (StringUtils.isEmpty(userName)) {
            return JSON.toJSONString(new Result<>().setCode(401).setMessage("未登录,请先登录"));
        }
        List<Permission> list = userService.getMenuList(userName);
        if (list == null) {
            return JSON.toJSONString(new Result<>().setCode(500).setMessage("获取错误"));
        } else {
            return JSON.toJSONString(new Result<>().setCode(200).setMessage("获取成功").setData(list));
        }
    }

    @ApiOperation("获取某省学生数")
    @RequestMapping(value = "/get-province-number", method = RequestMethod.GET)
    public String getProvinceNumber(String province) {
        return userService.getProvinceNumber(province);
    }

    @ApiOperation("获取所有省份")
    @RequestMapping(value = "/get-all-provinces", method = RequestMethod.GET)
    public String getAllProvinces() {
        return userService.getAllProvinces();
    }

    @OperationLog()
    @ApiOperation("忘记密码")
    @RequestMapping(value = "/update-password", method = RequestMethod.POST)
    public String updatePassword(String username, String name, String identity, String newPassword) {
        return userService.updatePassword(username, name, identity, newPassword);
    }

    @ApiOperation("获取男/女生数")
    @RequestMapping(value = "/select-student-number", method = RequestMethod.GET)
    public String getStudentNumber() {
        return userService.getStudentNumber();
    }

    @ApiOperation("身份证查询学号")
    @RequestMapping(value = "/find-name-by-identity", method = RequestMethod.POST)
    public String getUsername(@RequestBody User user) {
        return userService.getUsername(user);
    }

    @ApiOperation("查询办理状态")
    @RequestMapping(value = "/get-state", method = RequestMethod.POST)
    public String getState(@RequestBody User user) {
        return userService.getState(user);
    }

    @OperationLog()
    @ApiOperation("删除新生")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(String userName) {
        return userService.delete(userName);
    }

    @OperationLog()
    @ApiOperation("修改角色")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(String userName, String roleName) {
        return userService.updateRole(userName, roleName);
    }

    @OperationLog()
    @ApiOperation("新增管理角色")
    @RequestMapping(value = "/insert-manager", method = RequestMethod.POST)
    public String insertManager(String userName, String passWord, String roleName) {
        return userService.insertManager(userName, passWord, roleName);
    }

    @OperationLog()
    @ApiOperation("删除管理角色")
    @RequestMapping(value = "/delete-manager", method = RequestMethod.POST)
    public String deleteManager(String userName) {
        return userService.deleteManager(userName);
    }

    @ApiOperation("获取所有管理角色")
    @RequestMapping(value = "/get-manager", method = RequestMethod.GET)
    public String getManager() {
        return userService.getManager();
    }

    @ApiOperation("分页获取所有用户")
    @RequestMapping(value = "/get-user", method = RequestMethod.POST)
    public String findPage(@RequestBody PageRequest pageRequest) {
        return userService.findPage(pageRequest);
    }


}
